package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import com.sanqing.main.MyDocument;
import com.sanqing.model.Operater;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class AdminEditFrame extends JInternalFrame {
    Operater operater;
    JComboBox adminEditComBox;
    JTextField adminId = new JTextField(20);
    JTextField adminName = new JTextField(20);
    JTextField adminUname = new JTextField(20);
    JPasswordField adminPword = new JPasswordField(20);
    JButton submit, reset;

    public AdminEditFrame() {
        super();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        adminId.setFont(new Font("宋体", Font.PLAIN, 25));
        adminName.setFont(new Font("宋体", Font.PLAIN, 25));
        adminUname.setFont(new Font("宋体", Font.PLAIN, 25));
        adminPword.setFont(new Font("宋体", Font.PLAIN, 25));
        adminPword.setEchoChar('*');
        final BorderLayout borderLayout = new BorderLayout();           //边框布局管理器
        getContentPane().setLayout(borderLayout);           //设置布局
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭
        setTitle("修改人员信息");
        setBounds(100, 100, 700, 350); //设置窗体位置和大小
        final JPanel mainPanel = new JPanel();          //创建中心面板
        mainPanel.setBorder(new EmptyBorder(5, 0, 170, 0));    //设置边框
        final GridLayout gridLayout = new GridLayout(2, 6);  //创建表格布局管理器
        gridLayout.setVgap(2);          //设置组件之间的垂直距离
        gridLayout.setHgap(2);          //设置组件之间的平行距离
        mainPanel.setLayout(gridLayout);
        getContentPane().add(mainPanel);

        operater = new Operater();
        final JLabel adminIdLable = new JLabel("管理员编号:");
        adminIdLable.setFont(new Font("宋体", Font.PLAIN, 25));
        adminIdLable.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(adminIdLable);
        //创建廿号文本框 "
        adminId.setDocument(new MyDocument(13));                //书号文本框最大输入值为 13 ISBN.setColumns(13);//设蜜文本框长度
        //ISBN.addKeyListener(new ISBNkeyListener());                       //注册监听器 ISBN.addFocusListener(new ISBNFocusListener。);//注册监听器
        mainPanel.add(adminId);


        JLabel adminNameLabel = new JLabel("姓名:");
        adminNameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        adminNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //bookName.addActionListener();

        mainPanel.add(adminNameLabel);
        mainPanel.add(adminName);


        JLabel adminUnameLabel = new JLabel("级别:");
        adminUnameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        adminUnameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        AdminModel[] adminModels = new AdminModel[2];
        adminModels[0] = new AdminModel("管理员", 0);
        adminModels[1] = new AdminModel("操作员", 1);
        AdminComBoxMode adminComBoxMode = new AdminComBoxMode();
        adminComBoxMode.setA(adminModels);
        adminEditComBox = new JComboBox(adminComBoxMode);
        adminEditComBox.setFont(new Font("宋体", Font.PLAIN, 25));
        adminEditComBox.addItemListener(new AdminAddComBoxListener());
        //writeText.addActionListener()


        mainPanel.add(adminUnameLabel);
        mainPanel.add(adminEditComBox);

        JLabel adminPwordLable = new JLabel("密码:");
        adminPwordLable.setFont(new Font("宋体", Font.PLAIN, 25));
        adminPwordLable.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(adminPwordLable);
        mainPanel.add(adminPword);

        JPanel sumPanel = new JPanel();
        sumPanel.setLayout(new FlowLayout());
        submit = new JButton("搜索");
        reset = new JButton("更新");
        submit.setFont(new Font("宋体", Font.PLAIN, 25));
        reset.setFont(new Font("宋体", Font.PLAIN, 25));
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (adminId.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(AdminEditFrame.this, "请输入编号!");
                    return;
                } else {
                    operater = Dao.selectOperator(adminId.getText());
                    if (operater == null) {
                        JOptionPane.showMessageDialog(AdminEditFrame.this, "编号错误或不存在");
                    } else {
                        adminId.setText(operater.getId());
                        adminName.setText(operater.getName());
                        adminEditComBox.setSelectedIndex(operater.getGrade());
                        adminPword.setText("");
                    }
                }
                /*String id = adminId.getText();
                String name = adminName.getText();
                String uname = adminUname.getText();
                String pword = adminPword.getText();
                if ("".equals(id) || "".equals(name) || "".equals(uname) || "".equals(pword)) {
                    JOptionPane.showMessageDialog(new JPanel(), "数据不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                Dao.setValues(new String[]{id, name, uname, pword});
                Dao.setColumns(new String[]{"operator_id", "name", "admin", "`password`"});
                Dao.setForms(new String[]{"tb_operator"});
                if (Dao.executeInsert() == 1) {
                    JOptionPane.showMessageDialog(new JPanel(), "更新成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                    adminId.setText("");
                    adminName.setText("");
                    adminUname.setText("");
                    adminPword.setText("");
                }*/
            }

        });
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = adminId.getText();
                String name = adminName.getText();
                String uname = String.valueOf(operater.getGrade());
                String pword = adminPword.getText();
                if ("".equals(id) || "".equals(name) || "".equals(uname)) {
                    JOptionPane.showMessageDialog(new JPanel(), "数据不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                operater.setName(name);
                operater.setGrade(Integer.parseInt(uname));
                //进行md5加密
                byte[] digest = null;
                try {
                    MessageDigest md5 = MessageDigest.getInstance("md5");
                    digest = md5.digest(pword.getBytes("utf-8"));
                } catch (NoSuchAlgorithmException e1) {
                    e1.printStackTrace();
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }
                //16是表示转换为16进制数
                String md5Str = new BigInteger(1, digest).toString(16);
                if (!adminPword.getText().isEmpty()) {
                    operater.setPassword(md5Str);
                }
                Dao.setForms(new String[]{"tb_operator"});
                if (Dao.editOperator(operater)) {
                    JOptionPane.showMessageDialog(new JPanel(), "更新成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                    adminId.setText("");
                    adminName.setText("");
                    adminUname.setText("");
                    adminPword.setText("");
                }
            }
        });
        sumPanel.add(submit);
        sumPanel.add(reset);
        getContentPane().add(sumPanel, BorderLayout.SOUTH);
    }

    private class AdminAddComBoxListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            AdminModel adminModel = (AdminModel) e.getItem();
            operater.setGrade(adminModel.grade);
        }
    }

    private class AdminModel {
        String name;
        int grade;

        public AdminModel(String name, int grade) {
            this.name = name;
            this.grade = grade;
        }

        public String toString() {
            return name;
        }
    }

    private class AdminComBoxMode implements ComboBoxModel {
        AdminModel[] a = null;
        Object curSelect = null;

        public void setA(AdminModel[] a) {
            this.a = a;
        }

        @Override
        public void setSelectedItem(Object anItem) {
            curSelect = anItem;
        }

        @Override
        public Object getSelectedItem() {
            return curSelect;
        }

        @Override
        public int getSize() {
            if (a != null) {
                return a.length;
            }
            return 0;
        }

        @Override
        public Object getElementAt(int index) {
            return a[index];
        }

        @Override
        public void addListDataListener(ListDataListener l) {

        }

        @Override
        public void removeListDataListener(ListDataListener l) {

        }
    }
}
